﻿using DayuCode.Designer.Entity;
using DayuCode.Designer.Service;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DayuCode.Designer.Forms
{
    public partial class FormProjects : UIForm
    {
        public FormProjects()
        {
            InitializeComponent();
            dgvProjectUsers.AutoGenerateColumns = false;
            dgvProjects.AutoGenerateColumns = false;
        }

        private void FormProjects_Load(object sender, EventArgs e)
        {
            treePrjCatalogs.Nodes.Clear();
            fillProjectCatalogChildNodes(null);
        }

        private void btnReloadPrjCatalogs_Click(object sender, EventArgs e)
        {
            treePrjCatalogs.Nodes.Clear();
            fillProjectCatalogChildNodes(null);
        }

        /// <summary>
        /// 加载子目录节点
        /// </summary>
        private void fillProjectCatalogChildNodes(TreeNode node)
        {
            long catalogId = -1;
            bool loadChild = false;
            if (node != null)
            {
                var catalog = node.Tag as ProjectCatalog;
                if (catalog != null)
                {
                    catalogId = catalog.Id;
                    if (node.Nodes.Count == 1)
                    {
                        var cnode = node.Nodes[0];
                        if ("tmp".Equals(cnode.Tag))
                        {
                            cnode.Remove();
                            loadChild = true;
                        }
                    }
                }
            }
            else
            {
                loadChild = true;
            }
            if (loadChild)
            {
                var catalogs = ProjectCatalogHandler.getChildProjectCatalogs(catalogId);
                foreach (var catalog in catalogs)
                {
                    TreeNode catalogNode;
                    if (node == null)
                    {
                        catalogNode = treePrjCatalogs.Nodes.Add(catalog.Name) as TreeNode;
                    }
                    else
                    {
                        catalogNode = node.Nodes.Add(catalog.Name) as TreeNode;
                    }
                    catalogNode.Tag = catalog;
                    catalogNode.ImageIndex = 0;
                    catalogNode.SelectedImageIndex = 0;
                    catalogNode.Nodes.Add("tmp").Tag = "tmp";
                }
            }
        }

        private void treePrjCatalogs_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            ProjectCatalog pCatalog = e.Node.Tag as ProjectCatalog;
            if (pCatalog.LoadedChildren == true) return;
            fillProjectCatalogChildNodes(e.Node);
        }

        private void treePrjCatalogs_AfterSelect(object sender, TreeViewEventArgs e)
        {
            ProjectCatalog pCatalog = e.Node.Tag as ProjectCatalog;
            loadProjects(pCatalog.Id);
        }

        private void loadProjects(long catalogId)
        {
            IList<Project> projects = ProjectHandler.getProjects(catalogId);
            dgvProjectUsers.DataSource = null;
            dgvProjects.DataSource = projects;
            if(dgvProjects.Rows.Count > 0)
            {                            
                dgvProjects.SelectedIndex = 0;
            }
        }

        private void btnAddPrjCatalog_Click(object sender, EventArgs e)
        {
            showAddCatalog(1);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sblingOrChild">1平级 2下级</param>
        private void showAddCatalog(int sblingOrChild)
        {
            long pId = -1;
            TreeNode parentNode = null;
            int ordnum = 999;
            if (sblingOrChild == 1)
            {
                if (treePrjCatalogs.SelectedNode != null)
                {
                    ProjectCatalog sbingCatalog = treePrjCatalogs.SelectedNode.Tag as ProjectCatalog;
                    parentNode = treePrjCatalogs.SelectedNode.Parent;
                    pId = (long)sbingCatalog.ParentId;
                }
                if (parentNode != null)
                {
                    ordnum = (parentNode.Nodes.Count + 1) * 10;
                }
                else
                {
                    ordnum = (treePrjCatalogs.Nodes.Count + 1) * 10;
                }
            }
            else
            {
                if (treePrjCatalogs.SelectedNode != null)
                {
                    ProjectCatalog selectedCatalog = treePrjCatalogs.SelectedNode.Tag as ProjectCatalog;
                    parentNode = treePrjCatalogs.SelectedNode;
                    pId = (long)selectedCatalog.Id;
                }
                if (parentNode != null)
                {
                    ordnum = (parentNode.Nodes.Count + 1) * 10;
                }
                else
                {
                    ordnum = (treePrjCatalogs.Nodes.Count + 1) * 10;
                }
            }

            ProjectCatalog catalog = new ProjectCatalog();
            catalog.ParentId = pId;
            catalog.Ordnum = ordnum;

            FormProjectCatalog form = new FormProjectCatalog(catalog);
            form.Owner = this;
            form.ShowInTaskbar = false;
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                catalog = form.ProjectCatalog;
                TreeNodeCollection pNode = null;
                if (parentNode != null)
                {
                    pNode = parentNode.Nodes;
                }
                else
                {
                    pNode = treePrjCatalogs.Nodes;
                }
                TreeNode node = new TreeNode(catalog.Name);
                node.Tag = catalog;
                pNode.Add(node);
            }
            form.Dispose();
        }

        private void btnEditPrjCatalog_Click(object sender, EventArgs e)
        {
            if (treePrjCatalogs.SelectedNode == null)
            {
                return;
            }
            TreeNode node = treePrjCatalogs.SelectedNode;
            ProjectCatalog catalog = treePrjCatalogs.SelectedNode.Tag as ProjectCatalog;
            FormProjectCatalog form = new FormProjectCatalog(catalog);
            form.Owner = this;
            form.ShowInTaskbar = false;
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                catalog = form.ProjectCatalog;
                node.Text = catalog.Name;
                node.Tag = catalog;
            }
            form.Dispose();
        }

        private void btnDelPrjCatalog_Click(object sender, EventArgs e)
        {
            if (treePrjCatalogs.SelectedNode == null)
            {
                return;
            }
            TreeNode node = treePrjCatalogs.SelectedNode;
            ProjectCatalog catalog = treePrjCatalogs.SelectedNode.Tag as ProjectCatalog;
            if (ShowAskDialog("确认删除目录【"+ catalog.Name + "】吗？"))
            {
                ProjectCatalogHandler.deleteProjectCatalog(catalog.Id);
                node.Remove();
            }
        }

        private void dgvProjects_SelectIndexChange(object sender, int index)
        {
            dgvProjectUsers.DataSource = null;
            if (index < 0) return;
            Project project = dgvProjects.Rows[index].DataBoundItem as Project;
            if (project == null) return;
            IList<ProjectUser> users = ProjectUserHandler.getProjectUsers(project.Id);
            dgvProjectUsers.DataSource = users;
        }


        private void btnAddProject_Click(object sender, EventArgs e)
        {
            if (treePrjCatalogs.SelectedNode == null)
            {
                return;
            }
            ProjectCatalog catalog = treePrjCatalogs.SelectedNode.Tag as ProjectCatalog;

            FormProject form = new FormProject(0, catalog.Id);
            form.Owner = this;
            if(form.ShowDialog(this) == DialogResult.OK)
            {
                loadProjects(catalog.Id);
            }
            form.Dispose();
        }

        private void btnEditProject_Click(object sender, EventArgs e)
        {
            if (dgvProjects.SelectedRows.Count == 0) return;
            Project project = dgvProjects.SelectedRows[0].DataBoundItem as Project;
            FormProject form = new FormProject(project.Id, project.CatalogId);
            form.Owner = this;
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                loadProjects(project.CatalogId);
            }
            form.Dispose();
        }

        private void btnDelProject_Click(object sender, EventArgs e)
        {
            if (dgvProjects.SelectedRows.Count == 0) return;
            Project project = dgvProjects.SelectedRows[0].DataBoundItem as Project;
            if (ShowAskDialog("确认删除应用系统【" + project.Name + "】吗？"))
            {
                ProjectHandler.deleteProject(project.Id);
                loadProjects(project.CatalogId);
            }
        }

        private void btnReloadProjects_Click(object sender, EventArgs e)
        {
            if (treePrjCatalogs.SelectedNode == null)
            {
                return;
            }
            ProjectCatalog catalog = treePrjCatalogs.SelectedNode.Tag as ProjectCatalog;
            loadProjects(catalog.Id);
        }

        private void btnAddProjectUser_Click(object sender, EventArgs e)
        {
            if (dgvProjects.SelectedRows.Count == 0) return;
            Project project = dgvProjects.SelectedRows[0].DataBoundItem as Project;

            ProjectUser projectUser = new ProjectUser();
            projectUser.setRoleCode(true, false);
            projectUser.ProjectId = project.Id;

            FormProjectUser form = new FormProjectUser(projectUser);
            form.Owner = this;
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                IList<ProjectUser> users = ProjectUserHandler.getProjectUsers(project.Id);
                dgvProjectUsers.DataSource = users;
            }
            form.Dispose();
        }

        private void btnEditProjectUser_Click(object sender, EventArgs e)
        {
            if (dgvProjectUsers.SelectedRows.Count == 0) return;
            ProjectUser projectUser = dgvProjectUsers.SelectedRows[0].DataBoundItem as ProjectUser;

            FormProjectUser form = new FormProjectUser(projectUser);
            form.Owner = this;
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                IList<ProjectUser> users = ProjectUserHandler.getProjectUsers((long)projectUser.ProjectId);
                dgvProjectUsers.DataSource = users;
            }
            form.Dispose();
        }

        private void btnDelProjectUser_Click(object sender, EventArgs e)
        {
            if (dgvProjectUsers.SelectedRows.Count == 0) return;
            ProjectUser projectUser = dgvProjectUsers.SelectedRows[0].DataBoundItem as ProjectUser;
            if (ShowAskDialog("确认移除用户应用系统【" + projectUser.LoginId + "】吗？"))
            {
                ProjectUserHandler.deleteProjectUser((long)projectUser.Id);
                IList<ProjectUser> users = ProjectUserHandler.getProjectUsers((long)projectUser.ProjectId);
                dgvProjectUsers.DataSource = users;
            }
        }

        private void btnAddChildCatalog_Click(object sender, EventArgs e)
        {
            showAddCatalog(2);
        }
    }
}
